import modelExtend from 'dva-model-extend'
import {lwjPageModel} from 'utils/model'
import {payable} from './services/financialSettlementApply'
import {routerRedux} from 'dva/router';

export default modelExtend(lwjPageModel, {
    namespace: 'doSettlement4Payable',
    state: {
        settlementApplies: []
    },
    subscriptions: {
        setup({dispatch, history}) {
            history.listen((location) => {
                if (location.pathname === "/financialSettlementApply/doSettlement4Payable") {
                    dispatch({
                        type: 'updateState',
                        payload: {
                            settlementApplies: location.state.settlementApplies
                        }
                    })
                }
            })
        }
    },
    effects: {
        /**
         * 结算为应付
         */
        * payable({
            payload
        }, {select, call, put}) {
            const data = yield call(payable, payload);
            yield put(routerRedux.push('/success', {listUrl: '/financialSettlementApply',detailUrl:'/financialSettlementApply/detail/'+data.result}));
        }
    },
    reducers: {
        updateState(state, {payload}) {
            return {
                ...state,
                ...payload
            }
        }
    }
})
